﻿using System;
using System.Drawing;
using System.Windows.Forms;
using NDK.Module.DAL;
using libzkfpcsharp;
using System.Runtime.InteropServices;
using BZ.Logger;

namespace FingerPrintCollection
{
    public class FPFuction
    {
        static UserFPInfoServices userFPInfoServices = new UserFPInfoServices();
        public static BZLogger AppendUiLog;

        public struct FPInfo
        {
            public static string UserName;   //用户名
            public static string UserAuthority; //用户权限
            public static string UserFPFeatureData; //用户指纹特征
            public static Bitmap bmp;//指纹图片
            public static string ComparisonFPFeatureData; //要对比的指纹特征
            public static int RegisterPresstimes;//注册指纹时候按压的次数，一般是按压3次就合成数据
        }

        public static bool b_startScanFP;
        public static bool b_recognitioned; //是否识别成功
        //中控
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
        public const int MESSAGE_CAPTURED_OK = 0x0400 + 6;

        public static IntPtr mDevHandle = IntPtr.Zero;
        public static IntPtr mDBHandle = IntPtr.Zero;
        public static bool bIsTimeToDie = false;
        public static bool IsRegister = false;
        public static bool bIdentify = true;
        public static byte[] FPBuffer;
        public static int RegisterCount = 0;
        public const int REGISTER_FINGER_COUNT = 3;

        public static byte[][] RegTmps = new byte[3][];
        public static byte[] RegTmp = new byte[2048];
        public static byte[] CapTmp = new byte[2048];
        public static int cbCapTmp = 2048;
        public static int cbRegTmp = 0;
        public static int iFid = 1;

        public static int mfpWidth = 0;
        public static int mfpHeight = 0;
        //回调函数
        /// <summary>
        /// 指纹注册取模板的时候，当模板合成成功后调用此回调
        /// </summary>
        /// <param name="featureDataBuf"></param>
        /// <param name="featureSize"></param>
        /// <param name="pressTimes"></param>
        /// <param name="featureNum"></param>
        /// <param name="gType"></param>
        public static void aa_TemplateCallbackOfBae64(string featureDataBuf, short featureSize, short pressTimes, short featureNum, short gType)
        {
            FPInfo.UserFPFeatureData = featureDataBuf;
            FPInfo.RegisterPresstimes = pressTimes;
            AppendUiLog.Log("指纹注册 OK", LogType.Info);
            try
            {
                //将指纹数据存入数据库
                userFPInfoServices.InsertFPInfo2Table("UserFPInfo", FPInfo.UserName, FPInfo.UserAuthority, FPInfo.UserFPFeatureData);
                MessageBox.Show("指纹录入完毕!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception e)
            {
                AppendUiLog.Log(e.ToString(), LogType.Error);
            }
        }



        /// <summary>
        /// 指纹比对取特征的时候，当特征抽取成功后调用次回调
        /// </summary>
        /// <param name="featureDataBuf"></param>
        /// <param name="featureSize"></param>
        /// <param name="pressTimes"></param>
        /// <param name="featureNum"></param>
        /// <param name="gType"></param>
        public static void aa_FeatureCallbackOfBae64(string featureDataBuf, short featureSize, short pressTimes, short featureNum, short gType)
        {
            FPInfo.ComparisonFPFeatureData = featureDataBuf;
            b_recognitioned = true;
            AppendUiLog.Log("获取到了要比对的特征", LogType.Info);
        }


        //-------------指纹仪的函数
        /// <summary>
        /// 初始化指纹仪
        /// </summary>
        /// <returns>是否初始化成功，true成功，false失败</returns>
        public static bool InitFPDevice()
        {
            bool brtn = true;

            int ret = zkfperrdef.ZKFP_ERR_OK;
            if ((ret = zkfp2.Init()) == zkfperrdef.ZKFP_ERR_OK)
            {
                int nCount = zkfp2.GetDeviceCount();
                if (nCount > 0)
                {
                    brtn = true;
                }
                else
                {
                    zkfp2.Terminate();
                    //MessageBox.Show("Device not found!");
                    AppendUiLog.Log("FingerPrint  Device not found!", LogType.Error);
                    brtn = false;
                }
            }
            else
            {
                //MessageBox.Show("FingerPrint Device Initialize fail, ret=" + ret + " !");
                AppendUiLog.Log("FingerPrint Device Initialize fail, ret = " + ret + " !", LogType.Error);
                brtn = false;
            }

            return brtn;
        }

        public static bool OpenFPDevice()
        {
            bool brtn = true;

            int ret = zkfp.ZKFP_ERR_OK;
            if (IntPtr.Zero == (mDevHandle = zkfp2.OpenDevice(0)))
            {
                AppendUiLog.Log("Open fingerprint failed", LogType.Error);
                brtn = false;
            }
            if (IntPtr.Zero == (mDBHandle = zkfp2.DBInit()))
            {
                AppendUiLog.Log("Initial fingerprint algorithm library failed", LogType.Error);
                zkfp2.CloseDevice(mDevHandle);
                mDevHandle = IntPtr.Zero;
                brtn = false;
            }

            RegisterCount = 0;
            cbRegTmp = 0;
            iFid = 1;
            for (int i = 0; i < 3; i++)
            {
                RegTmps[i] = new byte[2048];
            }
            byte[] paramValue = new byte[4];
            int size = 4;
            zkfp2.GetParameters(mDevHandle, 1, paramValue, ref size);
            zkfp2.ByteArray2Int(paramValue, ref mfpWidth);

            size = 4;
            zkfp2.GetParameters(mDevHandle, 2, paramValue, ref size);
            zkfp2.ByteArray2Int(paramValue, ref mfpHeight);

            FPBuffer = new byte[mfpWidth * mfpHeight];

            return brtn;
        }

    }
}
